package cn.leonis.leetcode;

/**
 * @Author Tobi
 * @Date 2021-12-10 下午3:15
 * 两数之和2
 * leetcode167
 */
public class TwoSum2Solution {
    /**
     * 解法：双指针
     * @param numbers
     * @param target
     * @return
     */
    public int[] twoSum(int[] numbers, int target) {
        int left = 0, right = numbers.length - 1;
        while (left < right) {
            int sum = numbers[left] + numbers[right];
            if (sum == target) {
                return new int[]{left + 1, right + 1};
            } else if (sum < target) {
                ++left;
            } else {
                --right;
            }
        }

        return new int[]{-1, 1};
    }
}
